//
//  main.m
//  Perfect Province City District
//
//  Created by dllo on 16/7/12.
//  Copyright © 2016年 yh. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "TempCity.h"
#import "Province.h"
#import "City.h"
#import "District.h"


int main(int argc, const char * argv[]) {
    NSString *areaStr = [NSString stringWithContentsOfFile:@"/Users/dllo/DLS160710/Objective-C/HomeWork/Perfect Province City District/Perfect Province City District/Location1.txt" encoding:NSUTF8StringEncoding error:nil];
    NSArray *areaArr = [areaStr componentsSeparatedByString:@","];
    NSMutableArray *bigArr = [NSMutableArray array];
    for (NSString *str in areaArr) {
        NSArray *arr = [str componentsSeparatedByString:@"|"];
        TempCity *tempCity = [[TempCity alloc] initWithName:arr.firstObject number:arr.lastObject];
        [bigArr addObject:tempCity];
    }
    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"number" ascending:YES];
    [bigArr sortUsingDescriptors:@[sort]];
//    NSLog(@"%@", bigArr);
    NSMutableArray *allAreaArr = [NSMutableArray array];
    for (TempCity *tempCity in bigArr) {
        if ([tempCity.number hasSuffix:@"0000"]) {
            Province *province = [[Province alloc] initWithName:tempCity.name number:tempCity.number];
            [allAreaArr addObject:province];
        } else if ([tempCity.number hasSuffix:@"00"]) {
            City *city = [[City alloc] initWithName:tempCity.name number:tempCity.number];
            [[[allAreaArr lastObject] arr] addObject:city];
        } else {
            [[[[[allAreaArr lastObject] arr] lastObject] arr] addObject:tempCity];
        }
}
    NSLog(@"%@", allAreaArr);
    
    
    
    
    return 0;
}
